package com.zabra.zl.ui.runing.presenter;

import com.zabra.zl.base.BasePresenter;
import com.zabra.zl.data.RunningListBean;
import com.zabra.zl.data.RunningTypeBean;
import com.zabra.zl.ui.runing.contract.RunningContract;
import com.zabra.zl.ui.runing.model.RunningModel;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RunningPresenter extends BasePresenter<RunningContract.View> implements RunningContract.Presenter {
    private RunningContract.Model model;

    public RunningPresenter() {
        model = new RunningModel();
    }

    @Override
    public void getTypeData() {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getTypeData().enqueue(new Callback<RunningTypeBean>() {
            @Override
            public void onResponse(Call<RunningTypeBean> call, Response<RunningTypeBean> response) {
                mView.hideLoading();
                mView.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<RunningTypeBean> call, Throwable t) {
                mView.hideLoading();
                mView.onError(t);
            }
        });
    }

    @Override
    public void getData(RequestBody body) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getData(body).enqueue(new Callback<RunningListBean>() {
            @Override
            public void onResponse(Call<RunningListBean> call, Response<RunningListBean> response) {
                mView.hideLoading();
                mView.onSuccess(response.body(), response.headers().get("token"));
            }

            @Override
            public void onFailure(Call<RunningListBean> call, Throwable t) {
                mView.hideLoading();
                mView.onError(t);
            }
        });
    }

    @Override
    public void getDataFresh(RequestBody body) {
        if (!isViewAttached()) {
            return;
        }
        model.getData(body).enqueue(new Callback<RunningListBean>() {
            @Override
            public void onResponse(Call<RunningListBean> call, Response<RunningListBean> response) {
                mView.onSuccessFresh(response.body());
            }

            @Override
            public void onFailure(Call<RunningListBean> call, Throwable t) {
                mView.onError(t);
            }
        });
    }

    @Override
    public void getDataLoadMore(RequestBody body) {
        if (!isViewAttached()) {
            return;
        }
        model.getData(body).enqueue(new Callback<RunningListBean>() {
            @Override
            public void onResponse(Call<RunningListBean> call, Response<RunningListBean> response) {
                mView.onSuccessLoadMore(response.body());
            }

            @Override
            public void onFailure(Call<RunningListBean> call, Throwable t) {
                mView.onError(t);
            }
        });
    }
}
